<template>
  <div class="form-layout">
    <!--操作按钮-->
    <customSpace fullWidth style=" padding-bottom: 8px;">
      <template #content>
<!--        <customButton  title="保存" theme="warning" size="medium" @click="btnOp('save')"  help="保存配置信息"/>-->
        <customButton  title="获取配置" theme="primary" size="medium" @click="btnOp('get')"  help="获取配置信息"/>
      </template>
    </customSpace>

    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="cpItemList" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" ref="editForm">
        <template #content>
          <div id="box-server" class="box-title">服务信息</div>
          <customFormItem label="开发者ID" name="wxAppid" :parent="_self" :form="form" full-width disabled
                          appendMsg="设置与开发->开发接口管理->基本配置位置查看"/>
          <customFormItem label="开发者密码" name="wxSecret" :parent="_self" :form="form"  full-width disabled
                          appendMsg="设置与开发->开发接口管理->基本配置位置查看"/>
          <customFormItem label="后台服务域名" name="wxServerDomain" :parent="_self" :form="form"  full-width disabled
                          appendMsg="如 http://xx.com，微信消息跳转时使用"/>
          <customFormItem label="服务号名称" name="wxServerName" :parent="_self" :form="form"  full-width disabled />
          <customFormItem label="后台服务类型" name="wxServerType" :parent="_self" :form="form"  full-width disabled />
          <customFormItem label="测试用户openid" name="wxServerTestUserOpenId" :parent="_self" :form="form"  full-width disabled />
          <customFormItem label="测试用户说明" name="wxServerTestUseRemark" :parent="_self" :form="form"  full-width disabled />
          <customFormItem label="服务禁用" name="wxDisabled" :parent="_self" :form="form"  full-width disabled
                          :type="constCommon.formItemType.switch.name"
                          appendMsg="禁用后，将不再推送微信消息"/>
<!--          <customFormItem label="服务状态" name="wxServerOpen" :parent="_self" :form="form"  full-width
                          :type="constCommon.formItemType.switch.name"  appendMsg="开启后，将推送相关微信服务消息"  />-->


          <template v-for="config in cpPushConfigList" >
            <div :id="'box-' + config.key" class="box-title">{{config.title}}</div>
            <!--操作按钮-->
            <customSpace fullWidth v-if="true == config.flagDebug">
              <template #content>
                <customButton  title="测试推送" theme="warning" size="medium" @click="btnOp('testPush', config)"  help="获取配置信息"/>
                <small >{{testPushMsg}}</small>
              </template>
            </customSpace>
            <customFormItem label="触发条件" name="titleTrigger" :parent="_self" :form="config"  full-width disabled
            />
<!--            <customFormItem label="推送消息" name="msgPushOpen" :parent="_self" :form="config"  full-width
                            :type="constCommon.formItemType.switch.name"
                            appendMsg="开启后，将推送该消息"
            />-->
            <customFormItem label="跳转地址" name="toUrl" :parent="_self" :form="config"  full-width disabled
                            :type="constCommon.formItemType.inputText.name"
                            appendMsg="跳转地址，用户点击消息后跳转到哪里"
            />
            <customFormItem label="模板消息id" name="tempId" :parent="_self" :form="config"  full-width disabled
                            appendMsg="广告与服务->模板消息查看"
            />
            <customFormItem
              label="消息模板字段"
              name="configFieldList"
              :parent="_self"
              :form="config"
              :type="constCommon.formItemType.slot.name"
              fullWidth
            >
              <template #content>
                <FortenVxetableStatic :data="config.fieldList" :dataParent="config" :columns="columnList" :disabled="true" :toolbarHide="true"
                ></FortenVxetableStatic>
              </template>
            </customFormItem>
          </template>
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import FortenVxetableStatic from "@/components/common/FortenVxetableStatic.vue";

export default {
  help: '微信服务号-配置',
  data() {
    return {
      form: {
      },
      pushConfigList: [],

      columnList: [
        {field: 'title', title: '标题',  sortable: false,titleHelp: '仅显示使用，具体看微信消息模板那边的名称',editRender: {name: 'ElInput', attrs: {type: 'text'}}},
        {field: 'keyTemp', title: '消息模板字段', width: 150,  sortable: false,titleHelp: '看微信消息模板那边的字段名称',editRender: {name: 'ElInput', attrs: {type: 'text'}}},
        {field: 'keyData', title: '数据字段', width: 150,  sortable: false,titleHelp: '对应的数据字段名称',editRender: {name: 'ElInput', attrs: {type: 'text'}}},
        {
          field: 'isDateAll', title: '时间字段', width: 150, sortable: false,
          editRender: {name: 'ElSwitch'},
          formatter: 'formatSwitch',
          titleHelp: '勾选后，将格式化为yyyy-MM-dd HH:mm:ss',
        }
      ],
      testPushMsg: ''
    }
  },
  methods: {
    //回调函数
    getServerConfig() {
      this.utilVue.loadShow(true)
      this.form = {}
      this.pushConfigList = []
      this.$ajax.get(`/base/userwx/getServerConfig`).then((resp) => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.form = resp.data
          this.pushConfigList = resp.data.pushConfigList
          // resp.data.pushConfigList.forEach(item => {
          //   this.pushConfigList.push(JSON.parse(JSON.stringify(item)))
          // })
          console.log('this.pushConfigList', this.cpPushConfigList)
        }
      })
    },
    //回调函数
    testPush(config) {
      this.utilVue.loadShow(true)
      this.testPushMsg = ''
      this.$ajax.post(`/base/userwx/testPush`,config).then((resp) => {
        this.utilVue.loadHide()
        this.testPushMsg = resp.msg
      })
    },
    //操作
    btnOp(key, config) {
      switch (key){
        case 'get':{
          this.getServerConfig()
          break
        }
        case 'testPush':{
          this.utilMessage.confirm('是否向测试用户发送消息', () => {
            this.testPush(config)
          })
          break
        }
      }
    }
  },
  mounted() {
    this.getServerConfig()
  },
  props: {
  },
  components: { FortenVxetableStatic, customAnchor },
  computed: {
    cpItemList() {
      let list = [{ name: 'box-server', title: '服务信息' }]
      if (this.cpPushConfigList) {
        Object.keys(this.cpPushConfigList).forEach(key => {
          list.push({ name: 'box-' + this.cpPushConfigList[key].key, title: this.cpPushConfigList[key].title })
        })
      }

      return list
    },
    cpPushConfigList() {
      return this.pushConfigList
    }
  },
}
</script>


<style lang="scss" scoped>
.form-layout{
  .nav-left{
    width: 150px;
  }

  .form-right{
    left: 180px;
    max-height: calc(100% - 40px);
  }
}

</style>


